var express = require('express'),
    // swig = require('swig-templates'),
    // request = require('request'),
    wx = require('../base/wx'),
    // sha1 = require('node-sha1'),
    // crypto = require('crypto'),
    router = express.Router();

router.get('/', function(req, res, next) {
    wx.goAccessToken(req, res, function () {
        wx.getToken(req, function () {
            var kind = req.query.kind || 'cup',
                titles = "杯子DIY";
            if (kind == "bag") {
                titles = "包DIY";
            }
            res.render('page/diy', {
                title: "在线DIY",
                kind: kind,
                titles: titles
            });
        });
    });
});

router.get('/template/list', function(req, res, next) {
    var kind=req.query.kind||'cup',
        url=config.resourcePath.apiRoot + '/index/index/edit?user_id=' +
            (req.cookies.user_id ? req.cookies.user_id : '111') +
            '&'+kind+'_id=' + req.query.id + "&kind="+kind;
    kakarotto.request.get({
        url: url
    }, function(error, response, body) {
        try {
            var result = JSON.parse(body);
            if (result.code == 200) {
                res.send({
                    code: 200,
                    message: result.message,
                    kind:result.kind,
                    type: req.query.type
                });
            }
        } catch (e) {
            res.send({ code: 500, msg: "列表获取失败" });
        }
    });
    // res.render('page/diy', { title: "在线DIY" });
});
module.exports = router;
